// The Vue build version to load with the `import` command
// (runtime-only or standalone) has been set in webpack.base.conf with an alias.
import Vue from 'vue'
import App from './App'
import router from './router'

import ElementUI from 'element-ui';
import 'element-ui/lib/theme-chalk/index.css';
Vue.use(ElementUI);

import echarts from 'echarts'
Vue.prototype.$echarts = echarts;

import axios from 'axios'
import VueAxios from 'vue-axios'
Vue.use(VueAxios,axios);

// * http request 拦截器
axios.interceptors.request.use(
  config => {
    // * 判断是否存在token，如果存在的话，则每个http header都加上token
     let userInfo = JSON.parse(sessionStorage.getItem("userInfo"));
     if (userInfo && userInfo.token) {
        config.headers.token  = userInfo.token;
     }
      return config;
  },
  err => {
    return Promise.error(err);
  });

axios.interceptors.response.use(
  response => {
    if (response.data.errCode==401) {//token失效
      sessionStorage.removeItem("userInfo" );
      Vue.prototype.$message.error(response.data.message);
      setTimeout(function () {
        router.replace({path:"/login"})
      },1000);
    }
    return response
  },
  error => {
    return Promise.reject(error)
  }
);


Vue.config.productionTip = false;

/* eslint-disable no-new */
new Vue({
  router,
  el: '#app',
  components: { App },
  template: '<App/>'
})
